﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using CoolCode.Web.UI.Controls;
using CoolCode.Web.UI;
using CoolCode.Linq;
using CoolCode.Services;
using CoolCode.Entities;
using CoolCode;

namespace WebServices
{
    /// <summary>
    /// Summary description for ProductService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [ScriptService]
    public class ProductService : System.Web.Services.WebService
    {
        private IInspectionService _Service;
        public IInspectionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = ServiceFactory.Produce<IInspectionService>();
                }
                return _Service;
            }
        }

        [WebMethod]
        [ScriptMethod]
        public string Delete(int id)
        {
            Service.Delete(id);
            var p = new PageParam();
            var qb = QueryBuilder.Create<Inspection>()
                 .Like(c => c.Checker, "");
            var list = Service.Paging(qb, p);

            var manager =new  ViewManager<ProductList>();
            var control= manager.LoadViewControl("~/Controls/ProductList.ascx");
            control.Model = list;
            string html= manager.RenderView(control);
            return html;
        }


        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Spec(string prefixText, int count)
        { 
            return ListTips(prefixText, count, TipsType.ProductSpec.GetAttachedString());
        }


        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Checker(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Checker.GetAttachedString());
        }

        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Appearance(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Appearance.GetAttachedString());
        }
        
        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Degumming(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Degumming.GetAttachedString());
        }

        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Drawback(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Drawback.GetAttachedString());
        }

        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Interlayer(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Interlayer.GetAttachedString());
        }

        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Face(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Face.GetAttachedString());
        }

        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips_Burse(string prefixText, int count)
        {
            return ListTips(prefixText, count, TipsType.Burse.GetAttachedString());
        }


        [WebMethod]
        [ScriptMethod]
        public IList<object[]> ListTips( string prefixText, int count,string type)
        {
            ITipsService service =ServiceFactory.Produce<ITipsService>();
            prefixText = prefixText.Trim();
            bool encodedFlag = prefixText != string.Empty;
            var list = service.ListTips(type , prefixText);

            IList<object[]> items = new List<object[]>();

            foreach (var tip in list)
            {
                string text = tip.Text;
                string encoded = encodedFlag ?
                    text.Replace(prefixText, "<span class='highlightPrefix'>" + prefixText + "</span>") : text;
                items.Add(new object[] { HttpUtility.HtmlDecode(text), tip.Times, encoded });
            }

            return items;
        }
    }

}
